package LinkList;

public class zuoye implements IList{
    static class ListNode {
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    @Override
    public void addFirst(int data) {
        ListNode firstNode=new ListNode(data);
        firstNode.next=head;
        head=firstNode;
    }

    @Override
    public void addLast(int data) {
        ListNode lastNode=new ListNode(data);
        if(head==null){
            head=lastNode;
            return;
        }
        ListNode cur=head;
        while(cur.next!=null){
            cur=cur.next;
        }
        cur.next=lastNode;
        return;
    }

    @Override
    public void addIndex(int index, int data) {
        if(index<0||index>size()){
            System.out.println("插入位置不合法");
            return;
        }
        if(index==0){
            addFirst(data);
        }
        if(index==size()){
            addLast(data);
        }
        ListNode cur=head;
        int len=size();
        while(index-1!=0){
            cur=cur.next;
            index--;
        }
        ListNode ChaRu=new ListNode(data);
        ChaRu.next=cur.next;
        cur.next=ChaRu;
        return;
    }

    @Override
    public boolean contains(int key) {
        int len=size();
        ListNode cur=head;
        while(len--!=0){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        int len=size();
        ListNode cur=head;
        if(head==null){
            return;
        }
        if(head.val==key){
            head=head.next;
            return;
        }
        ListNode curN=findNodeOfKey(key);
        if(curN==null){
            return;
        }
        ListNode del=cur.next;
        cur.next=del.next;
    }

    private ListNode findNodeOfKey(int key){
        ListNode cur=head;
        while(cur.next!=null){
            if(cur.next.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {
        if(head==null){
            return;
        }
        ListNode prev=head;
        ListNode cur=head.next;
        while(cur!=null){
            if(cur.val==key){
                prev.next=cur.next;//prev位置始终没变
                cur=cur.next;
            }else{
                prev=cur;
                cur=cur.next;
            }
        }
        if(head.val==key){
            head=head.next;
        }
    }

    @Override
    public int size() {
        ListNode cur=head;
        int count=0;
        while(cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur=head;
        if(head==null){
            return;
        }
        while(cur!=null){
            ListNode curN=cur.next;
            cur.next=null;
            cur=curN;
        }
        head=null;//暴力解法
    }

    @Override
    public void display() {
        ListNode cur=head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
        return;
    }
}
